﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Candy.Web.Data;
using Candy.ViewModel;
using GalaSoft.MvvmLight.Threading;
using System.Linq;

namespace Candy.Infrastructure
{
    public class SLNavigationService : INavigationService
    {

        private Frame RootFrame
        {
            get
            {
                return (Application.Current.RootVisual as Home).Frame;
            }
        }

        public void Navigate(string pageName)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    RootFrame.Navigate(new Uri(pageName, UriKind.Relative));
                });
        }

        public bool CanGoBack
        {
            get { return RootFrame.CanGoBack; }
        }

        public void GoBack()
        {
            RootFrame.GoBack();
        }

        MembershipRequestWindow _membershiprequestWindow;
        public void ShowMembershipRequestWindow(SendMembershipRequestViewModel vm)
        {
            _membershiprequestWindow = new MembershipRequestWindow();
            _membershiprequestWindow.DataContext = vm;
            _membershiprequestWindow.Show();
        }

        public void CloseMembershipRequestWindow()
        {
            if (_membershiprequestWindow != null)
            {
                _membershiprequestWindow.Close();
                _membershiprequestWindow = null;
            }
        }

        FamilyEditWindow _familyEditor;
        public void ShowFamilyEditor(FamilyEditViewModel vm)
        {
            _familyEditor = new FamilyEditWindow();
            _familyEditor.DataContext = vm;
            _familyEditor.Show();
        }

        public void CloseFamilyEditor()
        {
            _familyEditor.Close();
            _familyEditor = null;
        }

        GeneEditWindow _geneEditor;
        public void ShowGeneEditor(GeneEditViewModel vm)
        {
            _geneEditor = new GeneEditWindow();
            _geneEditor.DataContext = vm;
            _geneEditor.Show();
        }

        public void CloseGeneEditor()
        {
            _geneEditor.Close();
            _geneEditor = null;
        }

        PhylogenyEditWindow _phylogenyEditor;
        public void ShowPhylogenyEditor(PhylogenyEditViewModel vm)
        {
            _phylogenyEditor = new PhylogenyEditWindow();
            _phylogenyEditor.DataContext = vm;
            _phylogenyEditor.Show();
        }

        public void ClosePhylogenyEditor()
        {
            _phylogenyEditor.Close();
            _phylogenyEditor = null;
        }


        LoginWindow _loginWindow;
        public void ShowLogin()
        {
            if (_loginWindow == null)
                _loginWindow = new LoginWindow();
            _loginWindow.Show();
        }


        public void CloseLogin()
        {
            _loginWindow.Close();
        }


        RegistrationWindow _registrationWindow;
        public void ShowRegistration()
        {
            if (_registrationWindow == null)
                _registrationWindow = new RegistrationWindow();
            _registrationWindow.Show();
        }

        public void CloseRegistration()
        {
            _registrationWindow.Close();
        }

        BlastSecondaryBaitsWindow _secBlastWindow;
        public void ShowSecondaryBlast(BlastSecondaryBaitsViewModel dataContext)
        {
            if (_secBlastWindow == null)
                _secBlastWindow = new BlastSecondaryBaitsWindow();
            _secBlastWindow.DataContext = dataContext;
            _secBlastWindow.Show();
        }

        public void CloseSecondaryBlast()
        {
            _secBlastWindow.Close();
        }

        ProjectLoaderWindow _projectLoaderWindow;
        public void ShowProjectLoader(ProjectEntityViewModel project)
        {
            if (_projectLoaderWindow == null)
                _projectLoaderWindow = new ProjectLoaderWindow();
            _projectLoaderWindow.DataContext = project;
            _projectLoaderWindow.Show();
        }

        public void CloseProjectLoader()
        {

            _projectLoaderWindow.Close();
        }


        MoveFamilyTargetWindow _moveWindow;
        public void ShowMoveToFamily(FamilyEditViewModel family)
        {
            if (_moveWindow == null)
                _moveWindow = new MoveFamilyTargetWindow();
            (_moveWindow.DataContext as MoveToFamilyViewModel).FamilyToMove = family.Entity;
            _moveWindow.Show();
        }

        public void ShowMoveToFamily(System.Collections.Generic.IEnumerable<GeneEditViewModel> genes)
        {
            if (_moveWindow == null)
                _moveWindow = new MoveFamilyTargetWindow();
            (_moveWindow.DataContext as MoveToFamilyViewModel).GenesToMove = genes.Select(x=>x.Entity).ToList();

            _moveWindow.Show();
        }

        public void CloseMoveToFamily()
        {
            _moveWindow.Close();
        }
    }
}
